﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "StructureInstanceBreakBehavior.h"
#include "IInstance.h"
#include "IDocument.h"
#include "IRegenerator.h"
#include "IJoinRelationshipBehavior.h"
#include "IElementJoin.h"
#include "IElementCut.h"
#include "IElementOpening.h"
#include "IBaseGraphicsElementShapeComponent.h"
#include "ICutRelationshipBehavior.h"
#include "IOpeningRelationshipBehavior.h"
#include "IElementPositionPoints.h"
#include "JoinCutInstanceUtils.h"
#include "AlgorithmMinimalDistance.h"
#include "ICurve3d.h"

#include "GbmpJoinUtils.h"
#include "GbmpGraphicsNodeUtils.h"
#include "IGeometryRelationshipComponent.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"
using namespace gcmp;

// 暂时处理JoinUtil废弃警告，后续JoinUtil私有化后把这里恢复

void gcmp::StructureInstanceBreakBehavior::BreakConcreteElement(const IDocument * pDoc, IElement * pBreakElement, IElement * pNewElement, double breakPtToStartPtRatio, bool isBreakElementRetainOldElementId)
{
    return;
}

void gcmp::StructureInstanceBreakBehavior::RebuildElementRelationship(gcmp::IDocument* pDoc, IElement* pBreakElement, IElement* pNewElement)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pDoc != nullptr, L"pDoc为空",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_VOID_UNLESS(pBreakElement != nullptr, L"pBreakElement为空",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_VOID_UNLESS(pNewElement != nullptr, L"pNewElement为空",L"GDMPLab",L"2024-03-30");
    IInstance *pBreakInstance = quick_cast<IInstance>(pBreakElement);
    IInstance *pNewInstance = quick_cast<IInstance>(pNewElement);
    if (pBreakInstance != nullptr && pNewInstance != nullptr)
    {
        pNewInstance->GetDocument()->GetRegenerator()->RegenerateElement(pNewInstance->GetElementId());
        RebuildInstanceOpenRelationshipBehavior(pDoc, pBreakInstance, pNewInstance);
        //需要先更新打洞关系，不然更新pBreakElement进行计算时，可能会报墙洞与墙GRep不相交
        pBreakElement->GetDocument()->GetRegenerator()->RegenerateElement(pBreakElement->GetElementId());
        RebuildInstanceJoins(pDoc, pBreakInstance, pNewInstance);
        RebuildElementCutRelationship(pDoc, pBreakInstance, pNewInstance);
    }
}

void gcmp::StructureInstanceBreakBehavior::RebuildInstanceEndJoin(gcmp::IDocument * pDoc, gcmp::IInstance * pBreakInstance, gcmp::IInstance * pNewInstance, gcmp::JoinPosition joinPosition)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pDoc != nullptr, L"pDoc为空",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_VOID_UNLESS(pBreakInstance != nullptr, L"pBreakInstance为空",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_VOID_UNLESS(pNewInstance != nullptr, L"pNewInstance为空",L"GDMPLab",L"2024-03-30");

    IGeometryRelationshipComponent* pBreakGeometryRepCpt = pBreakInstance->GetGeometryRelationshipComponent();
    if (!pBreakGeometryRepCpt) {
        return;
    }

    IGeometryRelationshipComponent* pNewGeometryRepCpt = pNewInstance->GetGeometryRelationshipComponent();
    if (!pNewGeometryRepCpt) {
        return;
    }

    const IJoinRelationshipBehavior* pJoinRelationshipBehavior = pBreakGeometryRepCpt->GetJoinRelationship();
    const IJoinRelationshipBehavior* pJoinRelationshipBehaviorNew = pNewGeometryRepCpt->GetJoinRelationship();
    if (!pJoinRelationshipBehavior || !pJoinRelationshipBehaviorNew)
        return;

    const std::vector<ElementId> endJoinIds = pJoinRelationshipBehavior->GetElementJoinIds(joinPosition);
    DBG_WARN_AND_RETURN_VOID_UNLESS(endJoinIds.size() <= 1, L"默认的连接不支持端点处多个连接",L"GDMPLab",L"2024-03-30");
    if (endJoinIds.empty())
        return;
    IElementJoin *pEndJoin = quick_cast<IElementJoin>(pDoc->GetElement(endJoinIds.at(0)));
    if (pEndJoin != nullptr)
    {
        const IBaseGraphicsElementShapeComponent* pNewInstanceShapeComponent = pNewInstance->GetBaseGraphicsElementShapeComponent();
        DBG_WARN_AND_RETURN_VOID_UNLESS(pNewInstanceShapeComponent, L"pNewInstanceShapeComponent为空",L"GDMPLab",L"2024-03-30");
        const IGraphicsElementShape *pNewBaseGRep = pNewInstanceShapeComponent->GetBaseGraphicsElementShape();
        DBG_WARN_AND_RETURN_VOID_UNLESS(pNewBaseGRep != nullptr, L"pNewGRep为空",L"GDMPLab",L"2024-03-30");

        ElementId otherId;
        if (pEndJoin->GetAttachedId() == pBreakInstance->GetElementId())
        {
            otherId = pEndJoin->GetAttachingId();
        }
        else
        {
            otherId = pEndJoin->GetAttachedId();
        }

        IInstance *pInstance = quick_cast<IInstance>(pDoc->GetElement(otherId));
        DBG_WARN_AND_RETURN_VOID_UNLESS(pInstance != nullptr, L"pInstance为空",L"GDMPLab",L"2024-03-30");
        
        const IBaseGraphicsElementShapeComponent* pInstanceShapeComponent = pInstance->GetBaseGraphicsElementShapeComponent();
        DBG_WARN_AND_RETURN_VOID_UNLESS(pInstanceShapeComponent, L"pInstanceShapeComponent为空",L"GDMPLab",L"2024-03-30");
        const IGraphicsElementShape* pBaseGRep = pInstanceShapeComponent->GetBaseGraphicsElementShape();

        if (pBaseGRep != nullptr)
        {
            if (GbmpGraphicsNodeUtils::IsIntersect(pDoc, pNewBaseGRep, pBaseGRep))
            {
                //替换成新的
                if (pEndJoin->GetAttachedId() == pBreakInstance->GetElementId())
                {
                    pEndJoin->SetAttachedId(pNewInstance->GetElementId());
                }
                else
                {
                    pEndJoin->SetAttachingId(pNewInstance->GetElementId());
                }

                if (joinPosition == JoinPosition::Start)
                {
                    IJoinRelationshipBehavior* pBreakJoinRelationship = pBreakGeometryRepCpt->GetJoinRelationship();
                    DBG_WARN_AND_RETURN_VOID_UNLESS(pBreakJoinRelationship, L"pBreakJoinRelationship为空",L"GDMPLab",L"2024-03-30");
                    pBreakJoinRelationship->DeleteElementJoinIdsAtStart(pBreakJoinRelationship->GetElementJoinIdsAtStart());

                    IJoinRelationshipBehavior* pNewJoinRelationship = pNewGeometryRepCpt->GetJoinRelationship();
                    DBG_WARN_AND_RETURN_VOID_UNLESS(pNewJoinRelationship, L"pNewJoinRelationship为空",L"GDMPLab",L"2024-03-30");
                    pNewJoinRelationship->AddElementJoinIdAtStart(pEndJoin->GetElementId());
                }
                else
                {
                    IJoinRelationshipBehavior* pBreakJoinRelationship = pBreakGeometryRepCpt->GetJoinRelationship();
                    DBG_WARN_AND_RETURN_VOID_UNLESS(pBreakJoinRelationship, L"pBreakJoinRelationship为空",L"GDMPLab",L"2024-03-30");
                    pBreakJoinRelationship->DeleteElementJoinIdsAtEnd(pBreakJoinRelationship->GetElementJoinIdsAtEnd());

                    IJoinRelationshipBehavior* pNewJoinRelationship = pNewGeometryRepCpt->GetJoinRelationship();
                    DBG_WARN_AND_RETURN_VOID_UNLESS(pNewJoinRelationship, L"pNewJoinRelationship为空",L"GDMPLab",L"2024-03-30");
                    pNewJoinRelationship->AddElementJoinIdAtEnd(pEndJoin->GetElementId());
                }
            }

            GbmpJoinUtils::CheckAndTryToReverseRelationshipOrDeleteJoin(pDoc, pEndJoin);
        }
    }
}

void gcmp::StructureInstanceBreakBehavior::RebuildInstanceMiddleJoin(gcmp::IDocument * pDoc, gcmp::IInstance * pBreakInstance, gcmp::IInstance * pNewInstance, const gcmp::ElementId & middleJoinId)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pDoc != nullptr, L"pDoc为空",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_VOID_UNLESS(pBreakInstance != nullptr, L"pBreakInstance为空",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_VOID_UNLESS(pNewInstance != nullptr, L"pNewInstance为空",L"GDMPLab",L"2024-03-30");
    IElementJoin *pMiddleJoin = quick_cast<IElementJoin>(pDoc->GetElement(middleJoinId));
    if (pMiddleJoin != nullptr)
    {
        const IBaseGraphicsElementShapeComponent* pNewInstanceShapeComponent = pNewInstance->GetBaseGraphicsElementShapeComponent();
        DBG_WARN_AND_RETURN_VOID_UNLESS(pNewInstanceShapeComponent, L"pNewInstanceShapeComponent为空",L"GDMPLab",L"2024-03-30");
        const IGraphicsElementShape *pNewBaseGRep = pNewInstanceShapeComponent->GetBaseGraphicsElementShape();
        DBG_WARN_AND_RETURN_VOID_UNLESS(pNewBaseGRep != nullptr, L"pNewGRep为空",L"GDMPLab",L"2024-03-30");
        ElementId otherId;
        if (pMiddleJoin->GetAttachedId() == pBreakInstance->GetElementId())
        {
            otherId = pMiddleJoin->GetAttachingId();
        }
        else
        {
            otherId = pMiddleJoin->GetAttachedId();
        }

        IInstance *pInstance = quick_cast<IInstance>(pDoc->GetElement(otherId));
        DBG_WARN_AND_RETURN_VOID_UNLESS(pInstance != nullptr, L"pInstance为空",L"GDMPLab",L"2024-03-30");
        
        const IBaseGraphicsElementShapeComponent* pInstanceShapeComponent = pInstance->GetBaseGraphicsElementShapeComponent();
        DBG_WARN_AND_RETURN_VOID_UNLESS(pInstanceShapeComponent, L"pInstanceShapeComponent为空",L"GDMPLab",L"2024-03-30");
        const IGraphicsElementShape* pBaseGRep = pInstanceShapeComponent->GetBaseGraphicsElementShape();


        IGeometryRelationshipComponent* pBreakGeometryRepCpt = pBreakInstance->GetGeometryRelationshipComponent();
        if (!pBreakGeometryRepCpt) {
            return;
        }

        IGeometryRelationshipComponent* pNewGeometryRepCpt = pNewInstance->GetGeometryRelationshipComponent();
        if (!pNewGeometryRepCpt) {
            return;
        }

        if (pBaseGRep != nullptr)
        {
            if (GbmpGraphicsNodeUtils::IsIntersect(pDoc, pNewBaseGRep, pBaseGRep))
            {
                //替换成新的
                if (pMiddleJoin->GetAttachedId() == pBreakInstance->GetElementId())
                {
                    pMiddleJoin->SetAttachedId(pNewInstance->GetElementId());
                }
                else
                {
                    pMiddleJoin->SetAttachingId(pNewInstance->GetElementId());
                }

                std::vector<ElementId> elementIds(1, pMiddleJoin->GetElementId());
                IJoinRelationshipBehavior* pJoinRelationshipBehavior = pBreakGeometryRepCpt->GetJoinRelationship();
                IJoinRelationshipBehavior* pJoinRelationshipBehaviorNew = pNewGeometryRepCpt->GetJoinRelationship();
                DBG_WARN_AND_RETURN_VOID_UNLESS(pJoinRelationshipBehavior && pJoinRelationshipBehaviorNew, L"IJoinRelationshipBehavior为空",L"GDMPLab",L"2024-03-30");
                pJoinRelationshipBehavior->DeleteElementJoinIdsAtMiddle(elementIds);
                pJoinRelationshipBehaviorNew->AddElementJoinIdAtMiddle(pMiddleJoin->GetElementId());
            }
        }
    }
}

void gcmp::StructureInstanceBreakBehavior::RebuildInstanceJoins(gcmp::IDocument * pDoc, gcmp::IInstance * pBreakInstance, gcmp::IInstance * pNewInstance)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pDoc != nullptr, L"pDoc为空",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_VOID_UNLESS(pBreakInstance != nullptr, L"pBreakInstance为空",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_VOID_UNLESS(pNewInstance != nullptr, L"pNewInstance为空",L"GDMPLab",L"2024-03-30");

    IGeometryRelationshipComponent* pBreakGeometryRepCpt = pBreakInstance->GetGeometryRelationshipComponent();
    DBG_WARN_AND_RETURN_VOID_UNLESS(pBreakGeometryRepCpt, L"pGeometryRepCpt为空",L"GDMPLab",L"2024-03-30");
    const IJoinRelationshipBehavior* pJoinRelationshipBehavior = pBreakGeometryRepCpt->GetJoinRelationship();
    if (pJoinRelationshipBehavior == nullptr)
    {
        return;
    }
    RebuildInstanceEndJoin(pDoc, pBreakInstance, pNewInstance, JoinPosition::Start);
    RebuildInstanceEndJoin(pDoc, pBreakInstance, pNewInstance, JoinPosition::End);
    std::vector<ElementId>joinIdsAtMiddle = pJoinRelationshipBehavior->GetElementJoinIdsAtMiddle();
    FOR_EACH(id, joinIdsAtMiddle)
    {
        RebuildInstanceMiddleJoin(pDoc, pBreakInstance, pNewInstance, id);
    }
}

void gcmp::StructureInstanceBreakBehavior::RebuildElementCutRelationship(gcmp::IDocument * pDoc, gcmp::IInstance * pBreakInstance, gcmp::IInstance * pNewInstance)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pDoc != nullptr, L"pDoc为空",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_VOID_UNLESS(pBreakInstance != nullptr, L"pBreakInstance为空",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_VOID_UNLESS(pNewInstance != nullptr, L"pNewInstance为空",L"GDMPLab",L"2024-03-30");

    IGeometryRelationshipComponent* pBreakGeometryRepCpt = pBreakInstance->GetGeometryRelationshipComponent();
    IGeometryRelationshipComponent* pNewGeometryRepCpt = pNewInstance->GetGeometryRelationshipComponent();
    if (!pBreakGeometryRepCpt || !pNewGeometryRepCpt)
        return;

    ICutRelationshipBehavior* pCutRelationshipBehavior = pBreakGeometryRepCpt->GetCutRelationship();
    ICutRelationshipBehavior* pCutRelationshipBehaviorNew = pNewGeometryRepCpt->GetCutRelationship();
    if (!pCutRelationshipBehavior || !pCutRelationshipBehaviorNew )
        return;

    const IBaseGraphicsElementShapeComponent* pNewInstanceShapeComponent = pNewInstance->GetBaseGraphicsElementShapeComponent();
    DBG_WARN_AND_RETURN_VOID_UNLESS(pNewInstanceShapeComponent, L"pNewInstanceShapeComponent为空",L"GDMPLab",L"2024-03-30");
    const IGraphicsElementShape *pNewBaseGRep = pNewInstanceShapeComponent->GetBaseGraphicsElementShape();
    DBG_WARN_AND_RETURN_VOID_UNLESS(pNewBaseGRep != nullptr, L"pNewGRep为空",L"GDMPLab",L"2024-03-30");

    std::vector<ElementId> newCutIds;
    std::vector<ElementId> cutIds = pCutRelationshipBehavior->GetElementCutIds();
    FOR_EACH(id, cutIds)
    {
        IElementCut *pElementCut = quick_cast<IElementCut>(pDoc->GetElement(id));
        if (pElementCut != nullptr)
        {
            ElementId otherId;
            if (pElementCut->GetCutteeId() == pBreakInstance->GetElementId())
            {
                otherId = pElementCut->GetCutterId();
            }
            else
            {
                otherId = pElementCut->GetCutteeId();
            }

            IInstance *pInstance = quick_cast<IInstance>(pDoc->GetElement(otherId));
            DBG_WARN_AND_RETURN_VOID_UNLESS(pInstance != nullptr, L"pInstance为空",L"GDMPLab",L"2024-03-30");
            const IBaseGraphicsElementShapeComponent* pInstanceShapeComponent = pInstance->GetBaseGraphicsElementShapeComponent();
            DBG_WARN_AND_RETURN_VOID_UNLESS(pInstanceShapeComponent, L"pInstanceShapeComponent为空",L"GDMPLab",L"2024-03-30");
            const IGraphicsElementShape* pBaseGRep = pInstanceShapeComponent->GetBaseGraphicsElementShape();

            if (pBaseGRep != nullptr)
            {
                if (GbmpGraphicsNodeUtils::IsIntersect(pDoc, pNewBaseGRep, pBaseGRep))
                {
                    if (pElementCut->GetCutterId() == pBreakInstance->GetElementId())
                    {
                        pElementCut->SetCutterId(pNewInstance->GetElementId());
                    }
                    else
                    {
                        pElementCut->SetCutteeId(pNewInstance->GetElementId());
                    }
                    DBG_WARN_AND_RETURN_VOID_UNLESS(pElementCut->GetCutteeId() != pElementCut->GetCutterId(), L"CuttedId和CuttingId不能是同一个Id。",L"GDMPLab",L"2024-03-30");
                    pCutRelationshipBehavior->DeleteElementCutId(pElementCut->GetElementId());
                    pCutRelationshipBehavior->AddElementCutId(pElementCut->GetElementId());
                }
            }
        }
    }
}

void gcmp::StructureInstanceBreakBehavior::RebuildInstanceOpenRelationshipBehavior(gcmp::IDocument * pDoc, gcmp::IInstance * pBreakInstance, gcmp::IInstance * pNewInstance)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pDoc != nullptr, L"pDoc为空",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_VOID_UNLESS(pBreakInstance != nullptr, L"pBreakInstance为空",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_VOID_UNLESS(pNewInstance != nullptr, L"pNewInstance为空",L"GDMPLab",L"2024-03-30");

    IGeometryRelationshipComponent* pGeometryRepCpt = pBreakInstance->GetGeometryRelationshipComponent();
    if (pGeometryRepCpt == nullptr)
        return;

    IOpeningRelationshipBehavior* pOpenBehavior = pGeometryRepCpt->GetOpeningRelationship();
    if (pOpenBehavior == nullptr)
        return;

    const IBaseGraphicsElementShapeComponent* pNewInstanceShapeComponent = pNewInstance->GetBaseGraphicsElementShapeComponent();
    DBG_WARN_AND_RETURN_VOID_UNLESS(pNewInstanceShapeComponent, L"pNewInstanceShapeComponent为空",L"GDMPLab",L"2024-03-30");
    const IGraphicsElementShape *pNewBaseGRep = pNewInstanceShapeComponent->GetBaseGraphicsElementShape();
    DBG_WARN_AND_RETURN_VOID_UNLESS(pNewBaseGRep != nullptr, L"pNewGRep为空",L"GDMPLab",L"2024-03-30");
    std::vector<ElementId> newOpenIds;
    if (pOpenBehavior == nullptr)
        return;
    std::vector<ElementId> openIds = pOpenBehavior->GetElementOpeningIds();
    FOR_EACH(id, openIds)
    {
        IElementOpening *pInstanceOpen = quick_cast<IElementOpening>(pDoc->GetElement(id));
        if (pInstanceOpen != nullptr)
        {
            ElementId otherId;
            if (pInstanceOpen->GetOpenedId() == pBreakInstance->GetElementId())
            {
                otherId = pInstanceOpen->GetOpeningId();
            }
            else  //不允许打断Opening对象
            {
                continue;
            }

            IInstance *pInstance = quick_cast<IInstance>(pDoc->GetElement(otherId));
            DBG_WARN_AND_RETURN_VOID_UNLESS(pInstance != nullptr, L"pInstance为空",L"GDMPLab",L"2024-03-30");
            
            const IElementPositionPoints* pCtrlPtBehavior = pInstance->GetPositionPoints();
            Vector3d pos = pCtrlPtBehavior ? pCtrlPtBehavior->GetControlPoint(0) : Vector3d::Zero;

            const IElementPositionPoints* pBreakCtrlPtBehavior = pBreakInstance->GetPositionPoints();
            if (pBreakCtrlPtBehavior && pBreakCtrlPtBehavior->GetControlPointCount() > 1) //针对墙
            {
                OwnerPtr<ICurve3d> oBreakInstancePosCurve = JoinCutInstanceUtils::GetPositionCurve(pBreakInstance);
                DBG_WARN_AND_RETURN_VOID_UNLESS(oBreakInstancePosCurve, L"无效oBreakInstancePosCurve",L"GDMPLab",L"2024-03-30");
                OwnerPtr<ICurve3d> oNewInstancePosCurve = JoinCutInstanceUtils::GetPositionCurve(pNewInstance);
                DBG_WARN_AND_RETURN_VOID_UNLESS(oNewInstancePosCurve, L"无效oNewInstancePosCurve",L"GDMPLab",L"2024-03-30");

                Vector3d pointProjection;
                double minDistanceBreak = 0.0;
                double minDistanceNew = 0.0;
                bool success1 = AlgorithmMinimalDistance::Calculate(oBreakInstancePosCurve.get(), pos, pointProjection, true, nullptr, minDistanceBreak);
                bool success2 = AlgorithmMinimalDistance::Calculate(oNewInstancePosCurve.get(), pos, pointProjection, true, nullptr, minDistanceNew);
                
                const IBaseGraphicsElementShapeComponent* pBreakInstanceShapeComponent = pBreakInstance->GetBaseGraphicsElementShapeComponent();
                DBG_WARN_AND_RETURN_VOID_UNLESS(pBreakInstanceShapeComponent, L"pBreakInstanceShapeComponent为空",L"GDMPLab",L"2024-03-30");
                const IGraphicsElementShape *pBreakInstanceGRep = pBreakInstanceShapeComponent->GetBaseGraphicsElementShape();
                DBG_WARN_AND_RETURN_VOID_UNLESS(pBreakInstanceGRep != nullptr, L"pBreakInstanceGRep为空",L"GDMPLab",L"2024-03-30");
                //此时的breakInstance尚未Regenerate，pBreakInstanceGRep仍为打断前的状态
                if ((!GbmpGraphicsNodeUtils::IsPointInsideOrOnBody(pDoc, pos, pNewBaseGRep) && GbmpGraphicsNodeUtils::IsPointInsideOrOnBody(pDoc, pos, pBreakInstanceGRep))
                    || (!GbmpGraphicsNodeUtils::IsPointInsideOrOnBody(pDoc, pos, pBreakInstanceGRep) && (minDistanceBreak <= minDistanceNew)))
                    continue;
            }
            else if (pBreakCtrlPtBehavior && pBreakCtrlPtBehavior->GetControlPointCount() <= 1)  //针对楼板
            {
                if (!GbmpGraphicsNodeUtils::IsPointInsideOrOnBody(pDoc, pos, pNewBaseGRep))
                    continue;
            }

            pInstanceOpen->SetOpenedId(pNewInstance->GetElementId());
            DBG_WARN_AND_RETURN_VOID_UNLESS(pInstanceOpen->GetOpenedId() != pInstanceOpen->GetOpeningId(), L"OpenedId和OpeningId不能为同一个Id。",L"GDMPLab",L"2024-03-30");
            pOpenBehavior->DeleteElementOpeningId(pInstanceOpen->GetElementId());

            IGeometryRelationshipComponent* pGeometryRepCpt = pNewInstance->GetGeometryRelationshipComponent();
            if (pGeometryRepCpt->GetOpeningRelationship() != nullptr)
            {
                pGeometryRepCpt->GetOpeningRelationship()->AddElementOpeningId(pInstanceOpen->GetElementId());
            }
            pInstance->SetHostInstanceId(pNewInstance->GetElementId());
        }
    }
}

